package com.zi.experttalk.experttalk.View;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.zi.experttalk.experttalk.Model.service.HttpManager;
import com.zi.experttalk.experttalk.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

/**
 * Created by Administrator on 2014/12/11.
 */
public class CouponDetailsActivity extends Activity {
    private Button coupon_details_back_bt;
    private TextView coupon_details_mName_textview;
    private TextView coupon_details_mAddress_textview;
    private TextView coupon_details_comment_textview;
    private LinearLayout coupon_details_comment_linear;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.coupon_details);

        coupon_details_back_bt=(Button)findViewById(R.id.coupon_details_back_bt);
        coupon_details_mName_textview=(TextView) findViewById(R.id.coupon_details_mName_textview);
        coupon_details_mAddress_textview=(TextView) findViewById(R.id.coupon_details_mAddress_textview);
        coupon_details_comment_textview=(TextView)findViewById(R.id.coupon_details_comment_textview);
        coupon_details_comment_linear=(LinearLayout)findViewById(R.id.coupon_details_comment_linear);

        /*
        返回上一活动
         */
        coupon_details_back_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        /*
        详情信息显示
         */
        final Intent intent=getIntent();
        String mName=intent.getStringExtra("mName");
        String mAddress= intent.getStringExtra("mAddress");
        final String mId= intent.getStringExtra("mId");
        coupon_details_mName_textview.setText(mName);
        coupon_details_mAddress_textview.setText(mAddress);
        //评论点击
        coupon_details_comment_linear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1=new Intent(CouponDetailsActivity.this,CouponCommentActivity.class);
                intent1.putExtra("youhui_id",mId);
                startActivity(intent1);
            }
        });


       /*
        获取评论数量
         */

        //请求参数
        HttpManager httpManager=new HttpManager();
        String yu_id=mId;
        String commentParam="{\"content\":\"\",\"pwd\":\"123456\",\"yh_id\":\""+yu_id+"\",\"act\":\"youhui_comment_list\",\"page\":\"1\",\"email\":\"cdm123\"}";
        try {
            httpManager.getHttpInfo(commentParam,new HttpManager.GetDataResultListener() {
                @Override
                public void onResult(String data) {
                    Log.d("comPara", data);
                    if(data!=null){
                        try {
                            commentNumber(data);
                        } catch (JSONException e) {
                            Toast.makeText(CouponDetailsActivity.this, "解析有误", Toast.LENGTH_SHORT).show();
                        }
                    }

                }
            });
        } catch (IOException e) {
            Toast.makeText(this,"网络请求失败",Toast.LENGTH_SHORT).show();
        }
    }
    private  void commentNumber(String comData) throws JSONException {
        JSONObject comObject=new JSONObject(comData);
        final JSONArray comArray=comObject.optJSONArray("item");
        Log.d("comArr",comArray.length()+"");
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                coupon_details_comment_textview.setText("评论("+(comArray.length())+")");
            }
        });
    }
}
